package controller

import (
	"TTMS/dao"
	"TTMS/model"
	"html/template"
	"net/http"
	"strconv"
	"time"
)

// ToComment 去写评论的页面
func ToComment(w http.ResponseWriter, r *http.Request) {
	//判断是否登录
	sess, flag := dao.IsLogin(r)
	if !flag {
		//前往登录页面
		Login(w, r)
		return
	}
	sess.IsLogin = flag
	//获取电影名称
	filmName := r.FormValue("filmName")
	sess.NowFilm = dao.CheckFilmByName(filmName)
	//获取收件人的ID
	sess.ReceivedID, _ = strconv.ParseInt(r.FormValue("receivedID"), 10, 0)
	//获取评论的类型
	sess.MessageType, _ = strconv.ParseInt(r.FormValue("messageType"), 10, 0)
	//解析模版
	t := template.Must(template.ParseFiles("views/pages/film/to_comment.html"))
	//执行
	t.Execute(w, sess)
}

// AddMessage 添加评论
func AddMessage(w http.ResponseWriter, r *http.Request) {
	//获取 sess
	sess, _ := dao.IsLogin(r)
	//获取电影名称
	filmName := r.FormValue("filmName")
	sess.NowFilm = dao.GetFilmsByName(filmName)
	//获取收件人的ID
	receivedID, _ := strconv.ParseInt(r.FormValue("receivedID"), 10, 0)
	//获取评论的类型
	messageType, _ := strconv.ParseInt(r.FormValue("messageType"), 10, 0)
	//获取评论的内容
	messageText := r.FormValue("messageText")
	message := &model.Message{
		FilmName:    filmName,
		Content:     messageText,
		SenderID:    sess.UserID,
		Sender:      sess.User,
		MessageType: messageType,
		ReceivedID:  receivedID,
		Time:        time.Now().Format("2006-01-06 15:04:05"),
	}
	dao.AddMessage(message)
	FilmPage(w, r)
}
